import { drawerAop } from "../drawer-aop";
import { BaseDrawInfo } from "../base";

export interface EllipseDrawInfo extends BaseDrawInfo {
  x: number; // 中心点x
  y: number; // 中心点y
  radiusX: number; // 横向半径
  radiusY?: number; // 纵向半径
  rotation?: number; // 旋转弧度
  startAngle?: number; // 开始绘制弧度
  endAngle?: number; // 结束绘制弧度
  counterclockwise?: boolean; // 逆时针绘制
}

export class EllipseHelper {
  static _helper: EllipseHelper = new EllipseHelper();
  static getInstance() {
    return this._helper;
  }

  protected constructor() {}

  @drawerAop({ dealStrokeFill: true })
  draw(ctx: CanvasRenderingContext2D, drawInfo: EllipseDrawInfo) {
    // 从新的位置开始画，如果没有 beginPath，会导致存在一条从之前位置到新位置的直线
    const { rotation, startAngle, endAngle, counterclockwise } = drawInfo;
    ctx.ellipse(
      drawInfo.x,
      drawInfo.y,
      drawInfo.radiusX,
      drawInfo.radiusY ?? drawInfo.radiusX,
      rotation ?? 0,
      startAngle ?? 0,
      endAngle ?? 2 * Math.PI,
      counterclockwise
    );
  }
}

export const ellipse = EllipseHelper.getInstance();
